from utils.base import BaseSerializer
from apps.reports.models import Report

from rest_framework import serializers


class ReportSerializer(BaseSerializer):

    success_percent = serializers.SerializerMethodField()
    error_percent = serializers.SerializerMethodField()
    fail_percent = serializers.SerializerMethodField()
    modules = serializers.SerializerMethodField()
    tags = serializers.SerializerMethodField()

    def get_success_percent(self, obj):
        return round((obj.success_case_number / obj.all_case_number) * 100, 2)

    def get_error_percent(self, obj):
        return round((obj.error_case_number / obj.all_case_number) * 100, 2)

    def get_fail_percent(self, obj):
        return round((obj.fail_case_number / obj.all_case_number) * 100, 2)

    def get_modules(self, obj):
        return obj.detail['module'].values()

    def get_tags(self, obj):
        return obj.detail['tag'].values()

    class Meta:
        model = Report
        fields = '__all__'
